#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
autor: joão pedro brites raposo alexandre
data criação: 2011/11/22
linguagem: python
modulo: csv
versão: v1.0
'''

import csv
import time


#class to operate with csv files
class Csv:

    #class contructor
    def __init__(self):
        pass

    #-----------------------------------------------------------------
    #method to create file from search data from database (switch case)
    #-----------------------------------------------------------------    
    def create_file_from_data_search(self, file_name, data_list, structure):
        
        if structure == 1:
            print 'criando ficheiro csv...'
            t1 = time.clock()
            file = csv.writer(open(file_name, 'wb'), delimiter=',')
            for row in data_list:
                file.writerow([row[2],row[1].encode('utf-8'),row[0].encode('utf-8')])
                pass
            t2 = time.clock()
            print 'tempo =', t2-t1
            print 'ficheiro ok.'         
            pass
        
        elif structure == 2:
            print 'criando ficheiro csv...'
            t1 = time.clock()
            file = csv.writer(open(file_name, 'wb'), delimiter=',')
            for row in data_list:
                file.writerow([row[2],row[1].encode('utf-8'),row[0]])
                pass
            t2 = time.clock()
            print 'tempo =', t2-t1
            print 'ficheiro ok.'              
            pass
        
        elif structure == 3:
            print 'criando ficheiro csv...'
            t1 = time.clock()
            file = csv.writer(open(file_name, 'wb'), delimiter=',')
            for row in data_list:
                file.writerow([row[3],row[2].encode('utf-8'),row[1].encode('utf-8'),row[0].encode('utf-8')])
                pass
            t2 = time.clock()
            print 'tempo =', t2-t1
            print 'ficheiro ok.'            
            pass
        
        elif structure == 4:
            print 'criando ficheiro csv...'
            t1 = time.clock()
            file = csv.writer(open(file_name, 'wb'), delimiter=',')
            for row in data_list:
                file.writerow([row[3],row[2].encode('utf-8'),row[1].encode('utf-8'),row[0]])
                pass
            t2 = time.clock()
            print 'tempo =', t2-t1
            print 'ficheiro ok.'
            pass
        
        elif structure == 5:
            print 'criando ficheiro csv...'
            t1 = time.clock()
            file = csv.writer(open(file_name, 'wb'), delimiter=',')
            for row in data_list:
                file.writerow([row[3],row[2].encode('utf-8'),row[1].encode('utf-8'),row[0].encode('utf-8')])
                pass
            t2 = time.clock()
            print 'tempo =', t2-t1 
            print 'ficheiro ok.'            
            pass
        
        elif structure == 6:
            print 'criando ficheiro csv...'
            t1 = time.clock()
            file = csv.writer(open(file_name, 'wb'), delimiter=',')
            for row in data_list:
                file.writerow([row[3],row[2].encode('utf-8'),row[1].encode('utf-8'),row[0]])
                pass
            t2 = time.clock()
            print 'tempo =', t2-t1
            print 'ficheiro ok.'             
            pass


    #-----------------------------------------------------------------
    #metodo para criar ficheiro csv com consulta de elementos tag
    #de ficheiros html
    #-----------------------------------------------------------------    
    def create_file_html_tag(self, file_name, data_label, data_values):

        print 'criando ficheiro csv...'
        file = csv.writer(open(file_name, 'wb'), delimiter=',')
        index = 0
        for row in data_label:
            #print row
            #print data_values[index]
            file.writerow([row,data_values[int(index)]])#,data_values[index].encode('utf-8')]
            index += 1
            pass
        print 'ficheiro ok.'


  
